﻿using Workflow.Application;
using Workflow.Domain;
using WorkflowCore.Interface;

namespace Workflow.Host
{
    public static class WorkflowExtend
    {
        public static void UseWorkflow(this WebApplication app) 
        {
           var host=app.Services.GetService<IWorkflowHost>();
            app.Lifetime.ApplicationStarted.Register(() =>
            {
                host.Start();
                var provider = app.Services.CreateScope().ServiceProvider;
                var service=provider.GetService<IWorkflowService>();
                var db=provider.GetService<WorkflowContext>();
                var model=db.definition.Where(x=>!x.IsDeleted).ToList();
                foreach (var m in model) 
                {
                    service.LoaderDefinition(m);
                }
            });

            app.Lifetime.ApplicationStopped.Register(() => 
            {
                host.Stop();
            });
        }
    }
}
